package com.example.supermarket.service;

import com.example.supermarket.entity.Card;
import com.example.supermarket.entity.Member;
import com.example.supermarket.mapper.CardMapper;
import com.example.supermarket.mapper.MemberMapper;
import com.example.supermarket.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Bosen
 * @date 2021/10/13 0:27
 */
@Service
public class AdminCardService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    CardMapper cardMapper;

    public String createCardId(int flag) {
        // 生成会员卡id，由六位随机数以及年月日时分秒组成
        String cardId = Math.round(Math.random()*(999999-100000)+100000) + DateUtil.getNMDHIS();
        // 加上等级前缀
        switch (flag) {
            case 0:
                cardId = "S" + cardId;
                break;
            case 1:
                cardId = "G" + cardId;
                break;
            case 2:
                cardId = "D" + cardId;
                default:
                    break;
        }
        return cardId;
    }

    public int delete(int id) {
        // 获取对应的会员卡信息
        Card card = cardMapper.findById(id);
        // 获取使用该会员卡的会员信息
        Member member = memberMapper.findByCardId(card.getCardId());
        // 会员信息不为空，证明该会员卡已被使用
        if (member != null) {
            memberMapper.updateCardIdById(member.getId(), null);
        }
        // 删除会员卡
        return cardMapper.delete(id);
    }
}
